<?php
/**
* @property CakePdf $CakePdfComponent */
App::uses('Folder', 'Utility');
App::uses('File', 'Utility');
class VouchersController extends AppController {

    public $components = array(
            'RequestHandler',
            'CakePdf.CakePdf' => array(
                'prefix' => 'mypdf',
                'orientation' => 'L'
            )
      );
    
    public $sN = 'Voucher'; 
    public $pN = 'voucher';
    public $lN = 'vouchers';   
    public function beforeFilter() {
        parent::beforeFilter();
        $this->set('sN', $this->sN);
        $this->set('pN', $this->pN);
        $this->set('lN', $this->lN); 
    }
    public function admin_index() {
        $this->loadModel("Customer");
        $values = $this->paginate($this->sN);
        $values  = Set::sort($values , '{n}.Voucher.created', 'desc');
      	$this->set('values', $values);
    }
    public function admin_add(){
        if(!empty($this->data)){
            $this->request->data["Voucher"]['created']=date("Y-m-d H:i:s");
            $this->request->data["Voucher"]['created_by']=  $this->Auth->user("id");
            $this->Voucher->create();
            if($this->Voucher->save($this->data)){
                $this->Session->setFlash("The voucher has been saved");
                $this->redirect(array("action"=>"index"));
            }
        }
    }
    public function admin_edit($id=null){
        $voucher = $this->Voucher->read(null,$id);
        if(!empty($this->data)){
            if($this->Voucher->save($this->data)){
                $this->Session->setFlash("The voucher has been saved");
                $this->redirect(array("action"=>"index"));
            }
        }
        if(empty($this->data)){
            $this->request->data = $voucher;
        }
        $this->set("voucher",$voucher);
    }
    public function admin_delete($id=null){
        if(!$id){
            $this->Session->setFlash(__('Invalid Voucher id', true));
            $this->redirect(array('controller' => 'vouchers', 'action' => 'index', 'admin' => true));
        }else{
            $this->Voucher->id = $id;
            if($this->Voucher->delete()) {
                $this->Session->setFlash(__('Voucher has been deleted', true));
                $this->redirect(array('controller' => 'vouchers', 'action' => 'index', 'admin' => true));
            }
        }
    }
    public function index() {
        $this->loadModel("Customer");
        $values = $this->paginate($this->sN,array('customer_id'=>  $this->Auth->user("id")));
        $values  = Set::sort($values , '{n}.Voucher.created', 'desc');
      	$this->set('values', $values);
    }
    public function download($voucher_id) {
        $this->print_voucher($voucher_id, "I");
    }
    
    public function mypdf_print($id = null,$mode="D") {
        $this->CakePdf->addStyle('pdf_style.css');
        $this->CakePdf->mode = $mode;
        $gift = $this->Voucher->read(null, $id);
        $this->CakePdf->setFilename(WWW_ROOT.'files/vouchers/'.$gift['Voucher']['code'].'.pdf');
        $this->CakePdf->setFilename($gift['Voucher']['code'].'.pdf');
        $this->Zend->writeGiftCode(WWW_ROOT.'files/vouchers/'.$gift['Voucher']['code'].'.png',$gift['Voucher']['code']);
        $this->set('gift', $gift);
    }
    public function admin_generate_code() {
        $this->autoRender=false;
        return $this->Emstraining->loyalty_voucher_number();
    }
    
}